<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关键词搜索">
              <a-input placeholder="请输入名称或编号" v-model="queryParam.queryName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="创建时间">
              <j-date placeholder="请选择创建时间" v-model="queryParam.queryTime"></j-date>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset"
                        style="margin-left: 8px;">重置</a-button>

              <a-button :href="exportFabric" style="margin-left: 8px" type="primary" icon="export">导出</a-button>

              <a-upload style="margin-left: 8px" name="file" :showUploadList="false"
                        :multiple="false"
                        :customRequest="myupload"
                        :action="importExcelUrl" @change="handleImportExcel">
            <a-button type="primary" icon="import"><upload-outlined/>导入</a-button>
          </a-upload>
            </span>
          </a-col>

          <a-button type="primary" @click="add_category(0)" icon="plus"
                    style="color: #F9D532;width: 128px;height: 36px;background: #333333;border-style: none;float: right;margin-right: 16px;">
            新增标准码
          </a-button>
        </a-row>
      </a-form>
    </div>


    <div style="background: #f2f2f2;height: 10px;width: auto;margin-top: 10px;margin-bottom: 30px;"></div>


    <div>
      <div style="border: none;background-color: #ffffff;padding-bottom: 3px;">
        <span style="font-size: 16px;float: left;color:#000000;font-weight: bold">标准码列表</span>
        <a-button :loading="loading_batch" type="primary" @click="open_tishiKuang()" icon="el-icon-delete"
                  style="margin-left: 20px" v-show="this.select_ids.length>0">
          批量删除
        </a-button>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange" style="margin-top: 6px;">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.img)" icon="img"/>
          </div>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <div align="center" style="align-content: center;margin-left: 30px;">
           <a @click="add_category(record)" style="float: left;margin-left: 10px;font-size: 14px;color: #d29328;">编辑</a>
           <a-menu-item style="margin-left: 10px;float: left;margin-top: 0px;">
            <a-popconfirm title="确定删除该标准码吗?" @confirm="() => delete_huchi(record.id)">
              <a style="font-size: 14px;color: #d29328;">删除</a>
            </a-popconfirm>
          </a-menu-item>
          </div>
        </span>
      </a-table>
    </div>

    <AddStandardCodeModule ref="modalForm" @ok="modalFormOk"></AddStandardCodeModule>
  </a-card>
</template>

<script>

import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import JDate from '@/components/jeecg/JDate.vue'
import '@/assets/less/TableExpand.less'
import AddStandardCodeModule from './modules/AddStandardCodeModule'
import {httpAction, postAction, getAction, getFileAccessHttpUrl} from '@/api/manage'

export default {
  name: "categoryinfo",
  mixins: [JeecgListMixin],
  components: {
    JDate,
    AddStandardCodeModule
  },
  data() {
    return {
      description: 'categoryinfo管理页面',
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中record*/
      selectionRows: [],
      select_ids:[],//选中的ids
      loading_batch:false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '标准码名称',
          align: "center",
          dataIndex: 'standardName'
        },
        {
          title: '编号',
          align: "center",
          dataIndex: 'code'
        },
        {
          title: '排序',
          align: "center",
          dataIndex: 'numbers'
        },
        {
          title: '备注',
          align: "center",
          dataIndex: 'remark'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/admin/testplate/query_standardCode_pagelist?category_id=" + this.$route.query.category_id + "&type=2",
        delete_leibie: "/admin/testplate/delete_standardCode",
        delete_batch_fuliao: "/admin/testplate/delete_batch_standcode",//批量删除
        exportSize: "help/exportSize", // 导出
        importsize: "importfu/importsize", // 导入
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importsize}`
    },
    exportFabric: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportSize}?categoryId=${this.$route.query.category_id}`
    }
  },
  methods: {
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      this.select_ids=[];
      for (var i=0;i<this.selectionRows.length;i++)
      {
        this.select_ids.push(this.selectionRows[i].id);
      }
    },
    open_tishiKuang() {
      this.$confirm('确定要批量删除这些数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.all_clean_standcode();
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      });
    },
    // 导入尺码excel
    myupload(data) {
      let fdata = new FormData()
      fdata.append('file', data.file)
      fdata.append('categoryId', this.$route.query.category_id)
      postAction(this.url.importsize, fdata).then(res => {
        if (res.code == '0') {
          this.$message.success("导入成功")
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    add_category(record) {
      this.$refs.modalForm.edit(record);
      if (record == '0') {
        this.$refs.modalForm.title = "新增标准码";
      } else {
        this.$refs.modalForm.title = "编辑标准码";
      }
      this.$refs.modalForm.disableSubmit = true;
    },
    delete_huchi(id) {
      let params = {id: id};
      httpAction(this.url.delete_leibie, params, "put").then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    all_clean_standcode(){
      this.loading_batch=true;
      let params = {ids: this.select_ids};
      httpAction(this.url.delete_batch_fuliao, params, "put").then((res) => {
        this.loading_batch=false;
        if (res.success) {
          this.select_ids=[];
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      });
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>